package cn.htsyb.synergy.utils;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

 /**
 * 
 * @author lakeside hubin_yuan.410@hotmail.com
 * @version V1.0.0
 * @since 1.0
 * @date 2022/5/11
 */
public class ImageUtil {

    /**
     * 指定长款缩放压缩图片
     *
     * @param imgFile
     * @param outImgFile
     * @param width
     * @param height
     * @param quality
     * @param hasRatio
     * @throws IOException
     */
    public static void qualityScaleImage(File imgFile, File outImgFile, Integer width, Integer height, Float quality, Boolean hasRatio) throws IOException {
        BufferedImage buffImage = ImageIO.read(imgFile);
        qualityBuffImage(scaleBuffImage(buffImage, width, height, hasRatio), outImgFile, quality);
    }

    /**
     * 按比例缩放压缩图片
     *
     * @param imgFile
     * @param outImgFile
     * @param zoom
     * @param quality
     * @throws IOException
     */
    public static void qualityImage(File imgFile, File outImgFile, Float zoom, Float quality) throws IOException {
        BufferedImage buffImage = ImageIO.read(imgFile);
        qualityBuffImage(scaleBuffImage(buffImage, zoom), outImgFile, quality);
    }

    /**
     * 压缩图片
     *
     * @param imgFile
     * @param outImgFile
     * @param quality
     * @throws IOException
     */
    public static void qualityImage(File imgFile, File outImgFile, Float quality) throws IOException {
        BufferedImage buffImage = ImageIO.read(imgFile);
        qualityBuffImage(scaleBuffImage(buffImage, buffImage.getWidth(), buffImage.getHeight(), false), outImgFile, quality);
    }

    /**
     * 压缩图像
     *
     * @param buffImage
     * @param outImgFile
     * @param quality
     * @throws IOException
     */
    public static void qualityBuffImage(BufferedImage buffImage, File outImgFile, Float quality) throws IOException {

      /*  JPEGImageEncoder imageEncoder = JPEGCodec.createJPEGEncoder(outputStream);
        JPEGEncodeParam param = imageEncoder.getDefaultJPEGEncodeParam(buffImage);
        param.setQuality(quality, true);
        imageEncoder.encode(buffImage, param);*/
        ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpg").next();
        try (FileOutputStream outputStream = new FileOutputStream(outImgFile);
             ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream)
        ) {
            imageWriter.setOutput(ios);
            JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam();
            jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
            jpegParams.setCompressionQuality(quality);
            IIOMetadata imageMetaData = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(buffImage), jpegParams);

            imageWriter.write(imageMetaData, new IIOImage(buffImage, null, null), null);
            imageWriter.dispose();
        }
    }


    /**
     * 缩放图像
     *
     * @param srcImage
     * @param width
     * @param height
     * @param hasRatio
     * @return
     */
    public static BufferedImage scaleBuffImage(BufferedImage srcImage, Integer width, Integer height, Boolean hasRatio) {

        /**优化图片等比例缩放*/
        if (hasRatio) {
            int srcWidth = srcImage.getWidth(null);
            int srcHeight = srcImage.getHeight(null);
            if (srcWidth < srcHeight) {
                height = srcHeight * width / srcWidth;
            } else {
                width = srcWidth * height / srcHeight;
            }
        }

        Image smoothImage = srcImage.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
        BufferedImage scaleImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = scaleImage.getGraphics();
        graphics.drawImage(smoothImage, 0, 0, width, height, Color.LIGHT_GRAY, null);
        graphics.dispose();

        return scaleImage;
    }

    /**
     * 按比例缩放图片
     *
     * @param srcImage
     * @param zoom
     * @return
     */
    public static BufferedImage scaleBuffImage(BufferedImage srcImage, Float zoom) {

        /**优化图片等比例缩放*/
        int width = (int) (srcImage.getWidth(null) * zoom);
        int height = (int) (srcImage.getHeight(null) * zoom);
        return scaleBuffImage(srcImage, width, height, false);

    }

   /* public static void main(String[] args) throws IOException {
        File originalImage = new File("E:\\archive\\history\\archive_description\\2019\\20190911112525315424\\20190911112603800424.jpg");
        qualityScaleImage(originalImage, new File("E:\\1207-0.jpg"), 1000, 1000, 0.7F, false);
        qualityImage(originalImage, new File("E:\\1207-0-1.jpg"), 0.7F, 0.7F);
        qualityImage(originalImage, new File("E:\\1207-0-2.jpg"), 0.7F);
    }*/

}
